package cn.juque.jdfsystem.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.juque.common.base.PageInfo;
import cn.juque.common.disruptor.DisruptorHandler;
import cn.juque.jdfsystem.enums.HttpMessageTypeEnum;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.exception.AppException;
import cn.juque.jdfsystem.cache.PlatformInfoCache;
import cn.juque.jdfsystem.disruptor.bo.SendMsgBodyBO;
import cn.juque.jdfsystem.disruptor.event.SendMsgDisruptorEventServiceImpl;
import cn.juque.jdfsystem.entity.PlatformInfo;
import cn.juque.jdfsystem.mapper.PlatformInfoMapper;
import cn.juque.jdfsystem.service.IPlatformInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JUQUE
 * @version 1.0.0
 * <ul>
 *  <li>simple-generator</li>
 *  <li>接入的平台信息</li>
 * </ul>
 * @date 2023-01-13 15:18:08
 **/
@Service("platformInfoService")
public class PlatformInfoServiceImpl implements IPlatformInfoService {

    @Resource
    private PlatformInfoMapper platformInfoMapper;

    @Resource
    private PlatformInfoCache platformInfoCache;

    @Resource
    private DisruptorHandler disruptorHandler;

    /**
     * 分页查询
     *
     * @param pageInfo     pageInfo
     * @param platformInfo platformInfo
     * @return DataGrid
     */
    @Override
    public List<PlatformInfo> listInfoWithPage(PageInfo pageInfo, PlatformInfo platformInfo) {
        Page<PlatformInfo> page = new Page<>(pageInfo.getPage(), pageInfo.getLimit());
        List<PlatformInfo> list = this.platformInfoMapper.queryInfoList(page, platformInfo);
        pageInfo.setTotal(page.getTotal());
        return list;
    }

    /**
     * 查询
     *
     * @param platformInfo platformInfo
     * @return List
     */
    @Override
    public List<PlatformInfo> listInfoWithoutPage(PlatformInfo platformInfo) {
        return this.platformInfoMapper.queryInfoList(platformInfo);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return PlatformInfo
     */
    @Override
    public PlatformInfo detail(String id) {
        return this.platformInfoMapper.selectById(id);
    }

    /**
     * 保存
     *
     * @param platformInfo platformInfo
     */
    @Override
    public void saveInfo(PlatformInfo platformInfo) {
        platformInfo.setSecret(IdUtil.simpleUUID());
        boolean flag = SqlHelper.retBool(this.platformInfoMapper.insert(platformInfo));
        if (!flag) {
            throw new AppException(MessageEnum.SAVE_FAIL);
        }
        this.platformInfoCache.update(platformInfo);
        this.sendMessage();
    }

    /**
     * 更新
     *
     * @param platformInfo platformInfo
     */
    @Override
    public void updateInfo(PlatformInfo platformInfo) {
        boolean flag = SqlHelper.retBool(this.platformInfoMapper.updateById(platformInfo));
        if (!flag) {
            throw new AppException(MessageEnum.UPDATE_FAIL);
        }
        this.platformInfoCache.update(platformInfo);
        this.sendMessage();
    }

    /**
     * 删除
     *
     * @param ids 主键列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteInfo(List<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        int count = this.platformInfoMapper.deleteBatchIds(ids);
        if (count != ids.size()) {
            throw new AppException(MessageEnum.DELETE_FAIL);
        }
        this.platformInfoCache.delete(ids);
        this.sendMessage();
    }

    /**
     * 发送通知
     */
    private void sendMessage() {
        SendMsgBodyBO<String> sendMsgBodyBO = new SendMsgBodyBO<>();
        sendMsgBodyBO.setBusinessEventService(SendMsgDisruptorEventServiceImpl.BEAN);
        sendMsgBodyBO.setMessageTypeEnum(HttpMessageTypeEnum.PLATFORM_CHANGE);
        this.disruptorHandler.publish(sendMsgBodyBO);
    }

}
